package com.richman.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

import com.richman.controller.DecreaseRatioClient;
import com.richman.ps38.entity.Event;
import com.richman.ps38.entity.ISNPS38Event;
import com.richman.ps38.entity.SendData;
import com.richman.view.HGPS38EventPane.MyTableModel;

public class HGPS38EventPane extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -88L;
	
	private  Vector<String[]> detailsData = new Vector<String[]>();
	
	//public Vector<Event> gameDetailsVec = new Vector<Event>(); 
	
	private int focusedRowIndex = -1;
	
	static boolean bet = false;
	
	private JPopupMenu m_popupMenu;
	private JMenuItem betWholeMenItem; 
	private JMenuItem betHalfMenItem;
	
	private String currentbetstr = "";

	
	MyTableModel tableMode = new MyTableModel();

	Vector<SendData> sendDatas = new Vector<SendData>();
	
	JTable table = null;

	/**
	 * Create the panel.
	 */
	public HGPS38EventPane() {
		setLayout(new BorderLayout(0, 0));

		
	    table = new JTable(tableMode);
	    
	    table.getColumnModel().getColumn(4).setPreferredWidth(300);
	    
	    table.setRowHeight(30);
	    
	    table.setFont(new java.awt.Font("黑体", Font.PLAIN, 15));
		
        table.addMouseListener(new java.awt.event.MouseAdapter() {  
            public void mouseClicked(java.awt.event.MouseEvent evt) {  
                jTable1MouseClicked(evt);  
            }  
        });  
        
        createPopupMenu();
	    
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);
		


	}
	
	
	private void createPopupMenu() {
        m_popupMenu = new JPopupMenu();            
        betWholeMenItem = new JMenuItem();  
        betWholeMenItem.setText("投注全场");  
        betWholeMenItem.addActionListener(new java.awt.event.ActionListener() {  
            public void actionPerformed(java.awt.event.ActionEvent evt) {  
                //该操作需要做的事              	
            	try{
	            	if(focusedRowIndex != -1 && focusedRowIndex < sendDatas.size()){
	            		SendData tmp = sendDatas.elementAt(focusedRowIndex);
	            		
	        			String leaguename = tmp.isnps38event.leagueName;
	        			String teams = tmp.isnps38event.homeTeam + "-vs-" + tmp.isnps38event.awayTeam;
	        			String time = tmp.isnps38event.eventTime;
	        			String pankou = tmp.isnps38event.wholeDXPPankou;
	        			String score = tmp.isnps38event.homeTeamScore + ":" + tmp.isnps38event.awayTeamScore;
	        			String betstr = leaguename + teams + time + pankou + score +  "全场";
	        			
	        			int n = JOptionPane.showConfirmDialog(null, "投注 " + teams + " 全场", "投注确认",
	    						JOptionPane.YES_NO_OPTION);
	    				if (n == JOptionPane.YES_OPTION) {					
	    					if(!tmp.isnps38event.wholeDXPPankou.equals("")) {
		            			DecreaseRatioClient.betThread.betVec.add(betstr);
		            		}
	    				}else{
	    					System.out.println("取消投注");
	    					
	    				}
	        			
	            		
	            		
	            	}
            	}catch(Exception e){
            		e.printStackTrace();
            	}
            }  
        });  
        
        betHalfMenItem = new JMenuItem();
        betHalfMenItem.setText("投注半场");
        betHalfMenItem.addActionListener(new java.awt.event.ActionListener() {  
            public void actionPerformed(java.awt.event.ActionEvent evt) {  
                //该操作需要做的事              	
            	try{
	            	if(focusedRowIndex != -1 && focusedRowIndex < sendDatas.size()){
	            		SendData tmp = sendDatas.elementAt(focusedRowIndex);
	            		
	        			String leaguename = tmp.isnps38event.leagueName;
	        			String teams = tmp.isnps38event.homeTeam + "-vs-" + tmp.isnps38event.awayTeam;
	        			String time = tmp.isnps38event.eventTime;
	        			String pankou = tmp.isnps38event.halfDXPPankou;
	        			String score = tmp.isnps38event.homeTeamScore + ":" + tmp.isnps38event.awayTeamScore;
	        			String betstr = leaguename + teams + time + pankou + score +  "半场";
	        			int n = JOptionPane.showConfirmDialog(null, "投注 " + teams + " 半场", "投注确认",
	    						JOptionPane.YES_NO_OPTION);
	    				if (n == JOptionPane.YES_OPTION) {					
	    					if(!tmp.isnps38event.halfDXPPankou.equals("")) {
		            			DecreaseRatioClient.betThread.betVec.add(betstr);
		            		}
	    				}else{
	    					System.out.println("取消投注");
	    					
	    				}
	            		
	            	}
            	}catch(Exception e){
            		e.printStackTrace();
            	}
            }  
        });  
        
        m_popupMenu.add(betWholeMenItem);
        m_popupMenu.add(betHalfMenItem);
    }
	
	
	   private void mouseRightButtonClick(java.awt.event.MouseEvent evt) {  
	       //判断是否为鼠标的BUTTON3按钮，BUTTON3为鼠标右键  
	       if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {  
	           //通过点击位置找到点击为表格中的行  
	           focusedRowIndex = table.rowAtPoint(evt.getPoint());  
	           if (focusedRowIndex == -1) {  
	               return;  
	           }  
	           //将表格所选项设为当前右键点击的行  
	           table.setRowSelectionInterval(focusedRowIndex, focusedRowIndex);  
	           
	           SendData tmp = sendDatas.elementAt(focusedRowIndex);

   				String teams = tmp.isnevent.homeTeam + "-vs-" + tmp.isnevent.awayTeam;
 				
   				currentbetstr = teams;
	           
	           m_popupMenu.show(table, evt.getX(), evt.getY());  
	       }  
	  
	   } 
	   
		private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {  
			  
		       mouseRightButtonClick(evt);  
		}  
	
	public void updateHGPS38EventTable(Vector<String[]> gamedetails){
		try{
			
			if(gamedetails==null) {
				return;
			}
			
			if(detailsData.size() != 0){
				detailsData.clear();
			}
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			for(int i = 0; i < gamedetails.size(); i++) {
				
				String[] game = gamedetails.elementAt(i);
				

				
				String[] item = {Integer.toString(i+1), game[0],game[1],game[2],game[3],game[4],game[5]};
				
				detailsData.add(item);
				
			}
			
			
			
			
			
			
			
			
			setOneRowBackgroundColor();
			
			tableMode.updateTable();

			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	
	
    public void setOneRowBackgroundColor() {
    	

    	
        try {  
        	
            DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
            	
            	
  
                public Component getTableCellRendererComponent(JTable table,  
                        Object value, boolean isSelected, boolean hasFocus,  
                        int row, int column) {  

                	String qishustr = detailsData.elementAt(row)[1];
                	int qishu = Integer.parseInt(qishustr);
                	
                    if (qishu%2==1) {  
                    	setBackground(new Color(246,246,246));  
                        
                    }else{  
                        setBackground(new Color(222,222,243));  
                    	//setBackground(new Color(1,1,1));  
                      
                    }  
                    
                    setHorizontalAlignment(JLabel.CENTER);
  
                    return super.getTableCellRendererComponent(table, value,  
                            isSelected, hasFocus, row, column);  
                }  
            };  
            int columnCount = table.getColumnCount();  
            for (int i = 0; i < columnCount; i++) {  
            	table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
            }  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
    
    }  
	
	
    public static void sortByEventTime(Vector<SendData> gamedetails) {
        Collections.sort(gamedetails, new Comparator<SendData>() {
            @Override
            public int compare(SendData event1, SendData event2) {
                return event1.isnps38event.eventTime.compareTo(event2.isnps38event.eventTime);
            }
        });
    }
	

    public class MyTableModel extends AbstractTableModel  
    {  
        /**
		 * 
		 */
		private static final long serialVersionUID = 10086L;
		/* 
         * 这里和刚才一样，定义列名和每个数据的值 
         */  
        String[] columnNames =  
        	{ "序号", "期数", "码", "降水网站个数", "降水网站", "更新时间","开奖结果"};
        

  
        /** 
         * 构造方法，初始化二维数组data对应的数据 
         */  
        public MyTableModel()  
        {  

        }  
  
        // 以下为继承自AbstractTableModle的方法，可以自定义  
        /** 
         * 得到列名 
         */  
        @Override  
        public String getColumnName(int column)  
        {  
            return columnNames[column];  
        }  
          
        /** 
         * 重写方法，得到表格列数 
         */  
        @Override  
        public int getColumnCount()  
        {  
            return columnNames.length;  
        }  
  
        /** 
         * 得到表格行数 
         */  
        @Override  
        public int getRowCount()  
        {         
	        if(null == detailsData){
	    		return 0;
	    	}
            return detailsData.size();  
        }  
  
        /** 
         * 得到数据所对应对象 
         */  
        @Override  
        public Object getValueAt(int rowIndex, int columnIndex)  
        {  
            //return data[rowIndex][columnIndex];
        	return detailsData.elementAt(rowIndex)[columnIndex];
        }  
  
        /** 
         * 得到指定列的数据类型 
         */  
        @Override  
        public Class<?> getColumnClass(int columnIndex)  
        {  
        	
        	return String.class;
        	
//        	if(detailsData.size() == 0){
//        		return String.class;
//        	}
//
//            return detailsData.elementAt(0)[columnIndex].getClass();
        	
        }  
  
        /** 
         * 指定设置数据单元是否可编辑.这里设置"姓名","学号"不可编辑 
         */  
        @Override  
        public boolean isCellEditable(int rowIndex, int columnIndex)  
        {  
            return false;
        }  
          
        /** 
         * 如果数据单元为可编辑，则将编辑后的值替换原来的值 
         */  
        @Override  
        public void setValueAt(Object aValue, int rowIndex, int columnIndex)  
        {  
            detailsData.elementAt(rowIndex)[columnIndex] = (String)aValue;  
            /*通知监听器数据单元数据已经改变*/  
            fireTableCellUpdated(rowIndex, columnIndex);  
        }  
        
        public void updateTable(){
        	fireTableDataChanged();
        }
        
  
    }  
}
